package com.abcdefg.java_chatroom.component;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;


import java.util.concurrent.ConcurrentHashMap;

@Component
public class OnlineUserMessage {
    private ConcurrentHashMap<Integer, WebSocketSession> onlineUsers = new ConcurrentHashMap<>();

    public void online(Integer userId, WebSocketSession session) {
        if(onlineUsers.get(userId) !=null){
            return;
        }
        onlineUsers.put(userId, session);
    }


    public void offline(Integer userId, WebSocketSession session) {
        if(onlineUsers.get(userId)==session){
            onlineUsers.remove(userId);
        }
    }

    public WebSocketSession getOnlineUser(Integer userId) {
        return onlineUsers.get(userId);
    }
}
